package cc.vv.btpartyjob.partyservice.activity

import android.support.v4.widget.SwipeRefreshLayout
import android.text.TextUtils
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.adapter.ServicePeopleAdapter
import cc.vv.btpartyjob.partyservice.binder.ServiceBaseBinder
import cc.vv.btpartyjob.partyservice.delegate.ServicePeopleSearchActivityDelegate
import cc.vv.btpartyjob.partyservice.inter.ItemClickInterface
import cc.vv.btpartyjob.partyservice.module.bean.SerPeopleBean
import cc.vv.btpartyjob.partyservice.module.bean.ServiceTypeBean
import cc.vv.btpartyjob.partyservice.module.resp.SerPeopleResponseObj
import cc.vv.btpartyjob.partyservice.module.resp.ServiceTypeResponseObj
import cc.vv.btpartyjob.partyservice.server.DataPartyServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 便民搜索搜索
 * Created by Chen.qi on 2018/10/29.
 */
class ServicePeopleSearchActivity : BaseActivityMVP<ServicePeopleSearchActivityDelegate>(), BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener, ItemClickInterface {

    /**
     * 适配器
     */
    private var mAdapter: ServicePeopleAdapter? = null
    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10
    /**
     * 搜索内容
     */
    private var searchText: String? = ""

    /**
     * 搜索类型
     */
    private var searchType: String? = ""

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<SerPeopleBean>? = null

    /**
     * 类型数据集合
     */
    private var typeList: ArrayList<ServiceTypeBean>? = null


    override fun initData() {
        typeList = ArrayList()
        mDataList = ArrayList()
        if (mAdapter == null) {
            mAdapter = ServicePeopleAdapter(R.layout.item_service_people)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList, false)
        }
        DataPartyServer.getServiceTypeList(true)

    }

    override fun bindEvenListener() {
        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //返回
            finish()
        }, R.id.rl_asps_leftLayout)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //搜索
            pageNo = 1
            searchType = ""
            searchText = viewDelegate?.getTextStr()
            if (TextUtils.isEmpty(searchText)) {
                viewDelegate?.setListViewShow(false)
                return@OnClickListener
            }
            viewDelegate?.setListViewShow(true)
            viewDelegate?.closeInputKeyboard(this)
            searchHttp()

        }, R.id.rl_asps_rightLayout)
    }

    private fun searchHttp() {
        DataPartyServer.getSerPeopleSearchList(pageNo, pageSize, searchText, searchType, true)
    }

    override fun clickItem(type: String?) {
        viewDelegate?.setListViewShow(true)
        pageNo = 1
        searchText = ""
        viewDelegate.setTextStr("")
        searchType = type
        searchHttp()
    }

    override fun getDelegateClass(): Class<ServicePeopleSearchActivityDelegate> {
        return ServicePeopleSearchActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ServiceBaseBinder()
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is SerPeopleResponseObj -> {
                viewDelegate?.stopRefreshing()
                if (null == obj.data) {
                    return
                }
                if (1 == pageNo) {
                    mDataList?.clear()
                    if (null != obj.data.records) {
                        mDataList!!.addAll(obj.data.records)
                    }
                    viewDelegate?.setAdapterData(mAdapter, mDataList, true)
                    mAdapter?.disableLoadMoreIfNotFullPage()
                } else {
                    if (mAdapter?.data?.size!! >= obj.data.total) {
                        //数据全部加载完毕
                        mAdapter?.loadMoreEnd(true)
                        mAdapter?.loadMoreEnd()
                    } else {
                        //成功获取更多数据
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        mAdapter?.setNewData(mDataList)
                        //加载数据
                        mAdapter?.disableLoadMoreIfNotFullPage()
                        mAdapter?.loadMoreComplete()
                    }
                }
            }

            is ServiceTypeResponseObj -> {
                //服务类型
                if (null == obj.data) {
                    return
                }
                typeList!!.addAll(obj.data)
                viewDelegate?.setTypeListData(typeList, this)

            }
        }

    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

    }

    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        searchHttp()
    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        searchHttp()
    }


}